package com.defangit.award.extend.wxmp.orm;

import com.defangit.award.extend.wxmp.domain.UserGift;
import laogen.hibernate.CommonFinder;
import laogen.hibernate.CommonService;
import laogen.lang.exception.ParameterNotValidException;
import laogen.util.StringUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

/**
 * Created by zhangfagen on 2019/2/2.
 */
public class WxmpUserGiftService extends CommonService<UserGift> {
    public UserGift addUserGift(UserGift gift){
        if(gift==null)
            throw ParameterNotValidException.nullParameter("要添加的红包记录");
        add(gift);
        commitSession();
        return gift;
    }
    public UserGiftFinder findAll(){
        return new UserGiftFinder();
    }
    public class UserGiftFinder extends CommonFinder<UserGift>{
        private DetachedCriteria criteria=detachedAll();
        @Override
        public Criteria getCriteria() {
            return criteria.getExecutableCriteria(getSession());
        }

        @Override
        public DetachedCriteria getDetachedCriteria() {
            return criteria;
        }

        public UserGiftFinder user(String openid){
            if(StringUtil.notEmpty(openid))
                criteria=criteria.add(Restrictions.eq("openid",openid));
            return this;
        }
    }
}
